<?php
namespace app\admin\controller;

use think\Db;
use think\Loader;
use think\Request;

class Auth extends Admin
{
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //  节点管理列表
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    public function RuleList()
    {
        // 并且每页显示10条数据
        $list = Db::name('AuthRule')->order('sort desc')->select();
        $list = model('Tree')->toFormatTree($list);
        $this->assign('list', $list);
        return $this->fetch('rule_list');
    }

    /*
     * 添加或修改节点
     * */
    public function RuleEdit()
    {
        $id = input('id', '', 'intval');
        if (Request::instance()->isPost()) {
            $validate = Loader::validate('AuthRule');
            $data = input('post.');
            $data['status']=isset($data['status'])?$data['status']:0;
            $data['is_show']=isset($data['is_show'])?$data['is_show']:0;
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            if ($data['id']) {
                $flag = Db::name('AuthRule')->update($data);
                $url = url('auth/RuleEdit', array('id' => $id));
            } else {
                unset($data['id']);
                $data['addtime'] = time();
                $flag = Db::name('AuthRule')->insert($data);
                $url = url('auth/RuleList');
            }
            if ($flag) {
                $this->success('操作成功', $url);
            } else {
                $this->error('操作失败');
            }
        }
        $menus = Db::name('AuthRule')->select();
        $data['menus'] = model('Tree')->toFormatTree($menus);
        $data['row'] = Db::name('AuthRule')->where('id', $id)->find();
        $this->assign($data);
        return $this->fetch('rule_edit');
    }

    /*
     * 设置节点是否禁用
     * */
    public function RuleSetStatus()
    {
        $id = input('post.id', '', 'intval');
        if (empty($id)) {
            $this->error('操作有误!');
        }
        $status = Db::name('AuthRule')->where('id', $id)->value('status');
        $status = $status ? 0 : 1;
        $flag = Db::name('AuthRule')->where('id', $id)->update(['status' => $status]);
        if ($flag) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }

    /*
     * 设置节点是否显示
     * */
    public function RuleSetShow()
    {
        $id = input('post.id', '', 'intval');
        if (empty($id)) {
            $this->error('操作有误!');
        }
        $is_show = Db::name('AuthRule')->where('id', $id)->value('is_show');
        $is_show = $is_show ? 0 : 1;
        $flag = Db::name('AuthRule')->where('id', $id)->update(['is_show' => $is_show]);
        if ($flag) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }

    /*
     * 删除节点
     * */
    public function RoleDel()
    {
        $id = input('post.id', '', 'intval');
        if (empty($id)) {
            $this->error('操作有误!');
        }
        if (Db::name('AuthRule')->where('pid', $id)->find()) {
            $this->error('删除失败，请先删除子节点');
        }
        $flag = Db::name('AuthRule')->delete($id);
        if ($flag) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }
}
